﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


class BufferConsole : IDisposable
{

    public const int BUFFER_SIZE = 25;
    private StringBuilder buffer;
    public StringBuilder Buffer
    {
        get { return buffer; }
        set { buffer = value; }
    }


    public BufferConsole()
    {
        this.Buffer = new StringBuilder(BUFFER_SIZE);
    }

    public void Write(string message)
    {
        int numOfChars = message.Length;
        if (numOfChars+ 2 + this.Buffer.Length >= BUFFER_SIZE)
        {
            this.Flush();
            Write(message);
        }
        else 
        {
            this.Buffer.Append(message +"\n");
        }
    }

    public  void Flush()
    {
        Console.WriteLine(this.Buffer);
        this.Buffer.Remove(0, this.Buffer.Length);
    }

      ~BufferConsole()
      {
        Dispose(false);
      }

      public void Dispose()
      {
        Dispose(true);
        GC.SuppressFinalize(this);
      }

      private void Dispose(bool disposing)
      {
          if (disposing)
          {
              Console.WriteLine(this.Buffer);
              this.Buffer.Remove(0, this.Buffer.Length);
          }
      }


    static void Main()
    {
        BufferConsole cons = new BufferConsole();
        using(cons)
        {
            cons.Write("alabala");
            cons.Write("portokala");
            cons.Write("ALOOOOOOOOOO");
            cons.Write("dispose");
            cons.Write("dispose");
        }
    }
}

